<?php
/**
*
* @package ppkBB3cker
* @version $Id: viewtopic_add2_torrent.php 1.000 2009-02-07 12:26:00 PPK $
* @copyright (c) 2008 PPK
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit;
}

foreach ($torrents_attach['torrent'] as $torrent_data)
{
	$req_upload=$req_upload_src_img=$req_upload_text='';
	if(!$ppkbb_tcrewr_updown[2] && !$is_canskiprequpload && $torrents[$torrent_data['attach_id']]['req_upload'])
	{
		if($user->data['user_uploaded'] < ($torrents[$torrent_data['attach_id']]['req_upload']*1024*1024*1024))
		{
			$req_upload_src_img=$phpbb_root_path . 'images/tracker/info_requpload_big.png';
			$req_upload_text=sprintf($user->lang['TORRENT_REQ_UPLOAD_1'], @$torrents[$torrent_data['attach_id']]['req_upload'], get_formatted_filesize($user->data['user_uploaded']));
			$req_upload=$forum_astracker ? '<img src="' .$req_upload_src_img. '" alt="'.$req_upload_text.'"  title="'. $req_upload_text .'"/>' : '';
			$torrent_link=0;
		}
		else
		{
			$req_upload_src_img=$phpbb_root_path . 'images/tracker/button_ok_requpload_big.png';
			$req_upload_text= sprintf($user->lang['TORRENT_REQ_UPLOAD_0'], @$torrents[$torrent_data['attach_id']]['req_upload'], get_formatted_filesize($user->data['user_uploaded']));
			$req_upload=$forum_astracker ? '<img src="' .$req_upload_src_img. '" alt="'.$req_upload_text.'"  title="'.$req_upload_text.'"/>' : '';
		}
	}

	$req_ratio=$req_ratio_src_img=$req_ratio_text='';
	if(!$ppkbb_tcrewr_updown[2] && !$is_canskipreqratio && $torrents[$torrent_data['attach_id']]['req_ratio']!=0.000)
	{
		if($user_ratio!='None.' && ($user_ratio < $torrents[$torrent_data['attach_id']]['req_ratio'] || $user_ratio=='Leech.' || $user_ratio=='Inf.'))
		{
			$req_ratio_src_img=$phpbb_root_path . 'images/tracker/info_reqratio_big.png';
			$req_ratio_text=sprintf($user->lang['TORRENT_REQ_RATIO_1'], @$torrents[$torrent_data['attach_id']]['req_ratio'], get_ratio_alias($user_ratio));
			$req_ratio=$forum_astracker ? '<img src="' .$req_ratio_src_img. '" alt="'. $req_ratio_text .'"  title="'. $req_ratio_text .'"/>' : '';
			$torrent_link=0;
		}
		else
		{
			$req_ratio_src_img=$phpbb_root_path . 'images/tracker/button_ok_reqratio_big.png';
			$req_ratio_text=sprintf($user->lang['TORRENT_REQ_RATIO_0'], @$torrents[$torrent_data['attach_id']]['req_ratio'], get_ratio_alias($user_ratio));
			$req_ratio=$forum_astracker ? '<img src="' .$req_ratio_src_img. '" alt="'. $req_ratio_text .'"  title="'. $req_ratio_text .'"/>' : '';
		}
	}

	$torrent_data['filesize'] = get_formatted_filesize($torrent_data['filesize']);
	$torrent_basename=utf8_basename(urldecode($torrent_data['real_filename']));
	$config['ppkbb_tcbonus_upvalue'] = get_formatted_filesize(!$config['ppkbb_tcbonus_upvalue'] ? @$torrents[$torrent_data['attach_id']]['size'] : $config['ppkbb_tcbonus_upvalue'] * 1024 * 1024);

	$freetorr_percent=$is_canusefree ? get_freetorr_percent($config['ppkbb_freetorr_percent'], @$torrents[$torrent_data['attach_id']]['free']) : 0;

	$torrent_src_link=append_sid("{$phpbb_root_path}download/file.$phpEx", 'id=' . (int) $torrent_data['attach_id'], true, ($torrent_data['is_orphan']) ? $user->session_id : false);
	$torrent_link ? $torrent_link=$torrent_src_link : $torrent_link='';

	$ppkbb_torrent_magnetlink=my_split_config($config['ppkbb_torrent_magnetlink'], 2, 'my_int_val');
	$ppkbb_tcguests_enabled=my_split_config($config['ppkbb_tcguests_enabled'], 2, 'my_int_val');
	$ppkbb_rtrack_enable=my_split_config($config['ppkbb_rtrack_enable'], 3, 'my_int_val');
	$magnet_src_img=$phpbb_root_path.'images/tracker/filesaveas_big.png';
	if($torrent_link && $ppkbb_torrent_magnetlink[0] && ($user->data['is_registered'] || (!$user->data['is_registered'] && $ppkbb_torrent_magnetlink[1] && $ppkbb_tcguests_enabled[0])))
	{
		if($user->data['is_registered'] )
	{
		if(!$user->data['user_passkey'])
		{
			$user->data['user_passkey']=create_passkey();
			}
		}
		else
		{
			$config['ppkbb_announce_url']='/tracker/announce2.'.$phpEx;
			$user->data['user_passkey']=$user->session_id;
		}

		$tracker_url=generate_board_url();
		if(!$config['ppkbb_announce_url'])
		{
			$config['ppkbb_announce_url']='/tracker/announce.'.$phpEx;
		}
		$rtracks=urlencode($tracker_url.$config['ppkbb_announce_url']."?passkey={$user->data['user_passkey']}");
		if($ppkbb_rtrack_enable[0] || $ppkbb_rtrack_enable[1])
		{
			require($phpbb_root_path.'tracker/include/rtrackfunc.'.$phpEx);
			$rtrack=get_rtrack($_SERVER['REMOTE_ADDR'], $ppkbb_rtrack_enable[0], $ppkbb_rtrack_enable[1], $torrent_id);
			$rtracks=magnet_rtrack_url($rtrack, $ppkbb_rtrack_enable[2]);
		}
		$magnet_src_link="magnet:?xt=urn:btih:".urlencode($torrents[$torrent_id]['info_hash'])."&dn=".urlencode($torrent_basename)."&xl=".$torrents[$torrent_id]['size']."&tr=".$rtracks;
		$magnet_link=sprintf(($ppkbb_torrent_magnetlink[0]==1 ? $user->lang['TORRENT_MAGNET'] : $user->lang['TORRENT_MAGNET2']), $magnet_src_link, $magnet_src_img);
		if(!$user->data['is_registered'])
		{
			$magnet_link=sprintf($user->lang['TORRENT_MAGNET2'], str_replace('?', '?magnet=1&amp;', $viewtopic_url), $magnet_src_img);
			$t_magnet=request_var('magnet', 0);
			if($t_magnet)
			{
				if($config['ppkbb_gtccleanup_interval'] && $dt - $config['ppkbb_gtracker_last_cleanup'] > $config['ppkbb_gtccleanup_interval']*3600)
				{
					$ppkbb_gtcsession_expire=my_split_config($config['ppkbb_gtcsession_expire'], 2, 'my_int_val');
					$sql_addon=$ppkbb_gtcsession_expire[0] ? $dt.'-user_time > '.$ppkbb_gtcsession_expire[0]*86400 : '';
					$ppkbb_gtcsession_expire[1] && $sql_addon ? $sql_addon.=' OR ' : '';
					$ppkbb_gtcsession_expire[1] ? $sql_addon.=$dt.'-user_last_time > '.$ppkbb_gtcsession_expire[1]*3600 : '';
					if($sql_addon)
					{
						$result=$db->sql_query("DELETE FROM ".TRACKER_GUESTS_TABLE." WHERE {$sql_addon}");
					}
					set_config('ppkbb_gtracker_last_cleanup', $dt, 1);
					//purge_tracker_config(true);
				}

				$config['ppkbb_announce_url']='/tracker/announce2.'.$phpEx;
				$user_passkey=$user->data['user_passkey']=$user->session_id;
				$ppkbb_rtrack_enable[1]=0;

				$result=$db->sql_query("SELECT user_id FROM ".TRACKER_GUESTS_TABLE." WHERE session_id='".$db->sql_escape($user_passkey)."' LIMIT 1");
				if(!$db->sql_fetchrow($result))
				{
					if($config['ppkbb_gtcmax_sessions'])
					{
						$result=$db->sql_query("SELECT DISTINCT(COUNT(session_id)) sessions FROM ".TRACKER_GUESTS_TABLE."");
						$sess_count=$db->sql_fetchrow($result);
						if(intval($sess_count['sessions']) >= $config['ppkbb_gtcmax_sessions'])
						{
							trigger_error($user->lang['TRACKER_GUESTSSESS_LIMIT']);
						}
					}
					$sql="INSERT INTO ".TRACKER_GUESTS_TABLE." (session_id, user_ip) VALUES('".$db->sql_escape($user_passkey)."', INET_ATON('".$db->sql_escape($_SERVER['REMOTE_ADDR'])."'))";
					$result=$db->sql_query($sql);
				}
				trigger_error(sprintf(($ppkbb_torrent_magnetlink[0]==1 ? $user->lang['TORRENT_MAGNET_GUEST'] : $user->lang['TORRENT_MAGNET2_GUEST']), $magnet_src_link, $viewtopic_url));
			}
		}
	}
	else
	{
		$magnet_link=$magnet_src_link='';
	}
	$torrent_shortname=$ppkbb_torrblock_width[2] && utf8_strlen($torrent_basename)>$ppkbb_torrblock_width[2] ? utf8_substr($torrent_basename, 0, $ppkbb_torrblock_width[2]).'...' : $torrent_basename;

	$template->assign_block_vars('postrow.torrent_fields', array(
		'TORRENT_DOWNLOAD_TXT' => sprintf(($torrent_link ? '<a href="'.$torrent_link.'" title="%1$s">'.$user->lang['DOWNLOAD_TORRENT'].'</a>' : '%1$s'), $torrent_basename),
		'TORRENT_DOWNLOAD_IMG' => sprintf(($torrent_link ? '<a href="'.$torrent_link.'" title="'.$user->lang['DOWNLOAD_TORRENT'].'">%1$s</a>' : '%1$s'), '<img alt="" src="' . $phpbb_root_path . 'images/tracker/filesave_big.png" alt="' . $user->lang['DOWNLOAD_TORRENT'] . '" />', $torrent_basename),
		//'TORRENT_DOWNLOAD_SRC_IMG' => $phpbb_root_path . 'images/tracker/filesave_big.png',
		//'TORRENT_FILENAME' => $torrent_shortname,
		//'TORRENT_SRC_LINK' => $torrent_link ? $torrent_src_link : '',

		//'TORRENT_MAGNET_LINK' => $magnet_link ? $magnet_src_link : '',
		'TORRENT_MAGNET_TXT'	=> $magnet_link ? $magnet_link : '',
		//'TORRENT_MAGNET_SRC_IMG'	=> $magnet_src_img,
		//'TORRENT_MAGNET_WINDOW' => $magnet_link ? ($ppkbb_torrent_magnetlink[0]==1 ? true : false) : '',

		'TORRENT_FILESIZE' => $torrent_data['filesize'],
		'TORRENT_ALTITLE' => $torrent_basename,
		'TORRENT_COMMENT' => $torrent_data['attach_comment'] ? '[<a class="my_tt" title="'.$torrent_data['attach_comment'].'">?</a>]' : '',
		'TORRENT_COMMENT_TEXT' => $torrent_data['attach_comment'] ? $torrent_data['attach_comment'] : '',
		'TORRENT_DOWNLOADED' => $torrent_data['download_count'],

		'TORRENT_UP' => $is_canuseup && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['upload'] ? '<img src="' . $phpbb_root_path . 'images/tracker/eqchem-big.png" alt="'. sprintf($user->lang['FORM_TORRENT_UP'], @$torrents[$torrent_data['attach_id']]['upload'], '%') .'"  title="'. sprintf($user->lang['FORM_TORRENT_UP'], @$torrents[$torrent_data['attach_id']]['upload'], '%') .'"/>' : '',
		//'TORRENT_UP_SRC_IMG' => $is_canuseup && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['upload'] ? $phpbb_root_path . 'images/tracker/eqchem-big.png' : '',
		//'TORRENT_UP_TEXT'	=> $is_canuseup && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['upload'] ? sprintf($user->lang['FORM_TORRENT_UP'], @$torrents[$torrent_data['attach_id']]['upload'], '%') : '',

		'TORRENT_FREE' => $is_canusefree && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['free'] && $freetorr_percent ? '<img src="' . $phpbb_root_path . 'images/tracker/'.$freetorr_percent.'_bookmark_big.png" alt="'. sprintf($user->lang['FREETORR_PERCENT'][$freetorr_percent].' - '.$user->lang['FORM_TORRENT_FREE'], @$torrents[$torrent_data['attach_id']]['free'], '%') .'" title="'. sprintf($user->lang['FREETORR_PERCENT'][$freetorr_percent].' - '.$user->lang['FORM_TORRENT_FREE'], @$torrents[$torrent_data['attach_id']]['free'], '%') .'"/>' : '',
		//'TORRENT_FREE_SRC_IMG' => $is_canusefree && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['free'] && $freetorr_percent ?  $phpbb_root_path . 'images/tracker/'.$freetorr_percent.'_bookmark_big.png' : '',
		//'TORRENT_FREE_TEXT' => $is_canusefree && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['free'] && $freetorr_percent ? sprintf($user->lang['FREETORR_PERCENT'][$freetorr_percent].' - '.$user->lang['FORM_TORRENT_FREE'], @$torrents[$torrent_data['attach_id']]['free'], '%') : '',

		'TORRENT_FORB' => $forum_astracker && @$torrents[$torrent_data['attach_id']]['forb'] > 0 ? '<img src="' . $phpbb_root_path . 'images/tracker/'.($torrents[$torrent_data['attach_id']]['forb']==1 ? '' : 'half').'encrypted_big.png" alt="' .sprintf($user->lang['FORM_TORRENT_FORB'], $user->lang['TRACKER_FORB_REASON'][$torrents[$torrent_data['attach_id']]['forb']]).'"  title="' . sprintf($user->lang['FORM_TORRENT_FORB'], ($torrents[$torrent_data['attach_id']]['forb_reason'] ? htmlspecialchars($torrents[$torrent_data['attach_id']]['forb_reason']) : $user->lang['TRACKER_FORB_REASON'][$torrents[$torrent_data['attach_id']]['forb']])).'" />' : '',
		//'TORRENT_FORB_SRC_IMG' => $forum_astracker && @$torrents[$torrent_data['attach_id']]['forb'] > 0 ? $phpbb_root_path . 'images/tracker/'.($torrents[$torrent_data['attach_id']]['forb']==1 ? '' : 'half').'encrypted_big.png' : '',
		//'TORRENT_FORB_TEXT' => $forum_astracker && @$torrents[$torrent_data['attach_id']]['forb'] > 0 ? sprintf($user->lang['FORM_TORRENT_FORB'], $user->lang['TRACKER_FORB_REASON'][$torrents[$torrent_data['attach_id']]['forb']]) : '',

		'TORRENT_REQ_UPLOAD' => $user->data['is_registered'] ? $req_upload : '',
		//'TORRENT_REQ_UPLOAD_SRC_IMG' => $user->data['is_registered'] ? $req_upload_src_img : '',
		//'TORRENT_REQ_UPLOAD_TEXT' => $user->data['is_registered'] ? $req_upload_text : '',

		'TORRENT_REQ_RATIO' => $user->data['is_registered'] ? $req_ratio : '',
		//'TORRENT_REQ_RATIO_SRC_IMG' => $user->data['is_registered'] ? $req_ratio_src_img : '',
		//'TORRENT_REQ_RATIO_TEXT' => $user->data['is_registered'] ? $req_ratio_text : '',

		'TORRENT_WAIT' => !$is_canskiprcheck && $user->data['is_registered'] && ($t_wait>=0 || $t_wait2 >= 0) ? '<img src="' . $phpbb_root_path . 'images/tracker/xclock_big.png" title="' . (($t_wait > 0 || $t_wait2 > 0) ? sprintf($user->lang['TORRENT_WAIT'], ($t_wait > $t_wait2 ? $t_wait : $t_wait2)) : $user->lang['TORRENT_WAIT_NEVER']).'" alt="" />' : '',
		//'TORRENT_WAIT_SRC_IMG' => !$is_canskiprcheck && $user->data['is_registered'] && ($t_wait>=0 || $t_wait2 >= 0) ? $phpbb_root_path . 'images/tracker/xclock_big.png' : '',
		//'TORRENT_WAIT_TEXT' => !$is_canskiprcheck && $user->data['is_registered'] && ($t_wait>=0 || $t_wait2 >= 0) ? (($t_wait > 0 || $t_wait2 > 0) ? sprintf($user->lang['TORRENT_WAIT'], ($t_wait > $t_wait2 ? $t_wait : $t_wait2)) : $user->lang['TORRENT_WAIT_NEVER']) : '',

		'TORRENT_BONUS' => $is_canusebonus && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['size'] > $config['ppkbb_tcbonus_fsize'] * 1024 * 1024 && $config['ppkbb_tcbonus_value'] > 0 ? '<img src="' . $phpbb_root_path . 'images/tracker/add_big.png" alt="' . sprintf($user->lang['TORRENT_BONUS'], $config['ppkbb_tcbonus_upvalue'], $config['ppkbb_tcbonus_value']) .'"  title="' . sprintf($user->lang['TORRENT_BONUS'], $config['ppkbb_tcbonus_upvalue'], $config['ppkbb_tcbonus_value']) .'"/>' : '',
		//'TORRENT_BONUS_SRC_IMG' => $is_canusebonus && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['size'] > $config['ppkbb_tcbonus_fsize'] * 1024 * 1024 && $config['ppkbb_tcbonus_value'] > 0 ? $phpbb_root_path . 'images/tracker/add_big.png' : '',
		//'TORRENT_BONUS_TEXT' => $is_canusebonus && $user->data['is_registered'] && @$torrents[$torrent_data['attach_id']]['size'] > $config['ppkbb_tcbonus_fsize'] * 1024 * 1024 && $config['ppkbb_tcbonus_value'] > 0 ?  sprintf($user->lang['TORRENT_BONUS'], $config['ppkbb_tcbonus_upvalue'], $config['ppkbb_tcbonus_value']) : '',
		)
	);
}
?>
